package com.qdxwx.data.service.article;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.article.ArticleMapper;
import com.qdxwx.models.article.Article;
import com.qdxwx.models.article.ArticleQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	private ArticleMapper articleMapper;

	@Override
	public int insert(Article article) {
		article.setCreateTime(LocalDateTime.now());
		int row = articleMapper.insert(article);
		return row > 0 ? articleMapper.insertContent(article) : 0;
	}

	@Override
	public int update(Article article) {
		article.setUpdateTime(LocalDateTime.now());
		int row = articleMapper.update(article);
		return row > 0 ? articleMapper.updateContent(article) : 0;
	}

	@Override
	public int delete(int articleId) {
		if(articleId < 1) return 0;
		int row = articleMapper.deleteById(articleId);
		return row > 0 ? articleMapper.deleteContent(articleId) : 0;
	}

	@Override
	public Article get(int articleId) {
		if (articleId < 1) return null;
		return articleMapper.selectById(articleId);
	}

	/**
	 * 公共多条件查询文章资讯列表,用于前台
	 */
	@Override
	public List<Article> getList(ArticleQuery query, int limit) {
		return articleMapper.selectList(query, new PageRequest(1, limit));
	}
	/**
	 * 分页查询文章列表
	 */
	@Override
	public List<Article> getList(ArticleQuery query, PageRequest page) {
		return articleMapper.selectList(query, page);
	}

	@Override
	public int count(ArticleQuery query) {
		return articleMapper.count(query);
	}


	@Override
	public int updateViewCount(int articleId) {
		if (articleId < 1) return 0;
		return articleMapper.updateCount(articleId, 1, "viewCount");
	}

	@Override
	public int updatePraiseCount(int articleId) {
		if (articleId < 1) return 0;
		return articleMapper.updateCount(articleId, 1, "praiseCount");
	}

	@Override
	public int updateCommentCount(int articleId) {
		if (articleId < 1) return 0;
		return articleMapper.updateCount(articleId, 1, "commentCount");
	}
}
